package com.snailvr.tv.presenter;


import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.snail.readyvr.util.EventBus;
import com.snailvr.tv.Definition;
import com.snailvr.tv.contract.ChannelContact;
import com.snailvr.tv.event.LoadFailureEvent;
import com.snailvr.tv.network.entity.ChannelInfoResponBean;
import com.snailvr.tv.network.entity.ChannelListResponBean;
import com.snailvr.tv.network.entity.PlayUrlResponBean;
import com.snailvr.tv.provider.ChanelInfoProvider;
import com.snailvr.tv.provider.ChanelListProvider;
import com.snailvr.tv.provider.IChanelInfoProvider;
import com.snailvr.tv.provider.IChanelsProvider;
import com.snailvr.tv.provider.RoomInfoProvider;

/**
 * Created by Administrator on 2016/9/2.
 */
public class ChannelPresenter implements ChannelContact.IChanelPresenter {

    private ChannelContact.IChanelView mChanelsView = null;
    private IChanelsProvider mChanelsProvider;
    private IChanelInfoProvider mChanelInfoProvider;
    private RoomInfoProvider mRoomInfoProvider;

    public ChannelPresenter() {
        mChanelsProvider = new ChanelListProvider();
        mChanelInfoProvider = new ChanelInfoProvider();
        mRoomInfoProvider = new RoomInfoProvider();
    }

    @Override
    public void registerView(ChannelContact.IChanelView _View) {
        if (_View == null) {
            throw new NullPointerException("View to register must not be null.");
        }
        this.mChanelsView = _View;
        EventBus.getDefault().register(this);
    }

    @Override
    public void unregisterView() {
        this.mChanelsView = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void loadChanelList(int page_size, int page, boolean isLoadMore, boolean needClear) {
        mChanelsProvider.getChanels(page_size, page, Definition.tag,isLoadMore, needClear);
    }


    @Override
    public void loadChanelInfo(int channel_id) {
        mChanelInfoProvider.getChanelInfo(channel_id);
    }

    @Override
    public void loadDefaultChannelInfo() {
        mChanelInfoProvider.getDefaultInfo(Definition.tag);
    }

    @Override
    public void loadPlayUrl(int room_id) {
        mRoomInfoProvider.getPlayUrl(room_id);
    }

    @Override
    public void switchChannel(int channle_id, String type) {
        mChanelInfoProvider.switchChannel(channle_id,type);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,  tags = {@Tag(Definition.BusTag.CHANNLE_LIST_TAG)})
    public void onEvent(ChannelListResponBean event) {
        if (mChanelsView!=null)
            mChanelsView.showChanelList(event);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD,tags = {@Tag(Definition.BusTag.CHANNLE_LIST_TAG),@Tag(Definition.BusTag.CHANNLE_INFO_TAG)})
    public void onFailureEvent(LoadFailureEvent event) {
        if (mChanelsView!=null)
            mChanelsView.showLoadFailureMsg(event.code,event.errorMsg);
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(Definition.BusTag.CHANNLE_INFO_TAG)})
    public void onChannelInfoEvent(ChannelInfoResponBean event) {
        if (mChanelsView!=null)
            mChanelsView.showChanelInfo(event.getData());
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(Definition.BusTag.PLAYING_TAG)})
    public void onPlayingEvent(PlayUrlResponBean event) {
        if (mChanelsView!=null)
            mChanelsView.showPlayings(event);
    }

}
